package cn.zr.aitool.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengrui on 2016/1/28.
 */
public class DbBaseManager {

    protected Context mContext;

    public void setContext(Context context) {
        this.mContext = context;
    }

    public String tableName;

    public DbBaseManager(String tableName){
        this.tableName=tableName;
    }

    /**
     * @param selectionRow
     * @param selectionArg
     * @return
     */
    public Cursor queryBy1Selecion(Context context, String selectionRow, String selectionArg) {
        String[] selectionRows = new String[]{selectionRow};
        String[] selectionArgs = new String[]{selectionArg};
        return queryByTwoSelecionByAnd(context,selectionRows, selectionArgs,null,null);
    }


    /**
     * @return
     */
    public Cursor queryAll(Context context) {
        return queryByTwoSelecionByAnd(context,null, null,null,null);
    }

    /**
     * @param selectionRow =null 查询所有 String[]{row1,row2}
     * @param selectionArgs String[]{arg1,arg2}
     * relationship and
     * @return
     */
    public Cursor queryByTwoSelecionByAnd(Context context, String[] selectionRow, String[] selectionArgs, String groupBy, String order) {

        return queryByTwoSelecionBase(context, selectionRow, selectionArgs, groupBy, null, order, null, DbUtils.TPYE_AND);

    }

    /**
     * @param selectionRow =null 查询所有 String[]{row1,row2}
     * @param selectionArgs String[]{arg1,arg2}
     * relationship or
     * @return
     */
    public Cursor queryByTwoSelecionByOr(Context context, String[] selectionRow, String[] selectionArgs, String groupBy, String order) {

        return queryByTwoSelecionBase(context, selectionRow, selectionArgs, groupBy, null, order, null, DbUtils.TPYE_OR);

    }

    /**
     * @param selectionRow =null 查询所有 String[]{row1,row2}
     * @param selectionArgs     String[]{arg1,arg2}
     * @param type   搜索条件间关系 1：and，2 or
     * @return
     */
    public Cursor queryByTwoSelecionBase(Context context, String[] selectionRow, String[] selectionArgs, String groupby, String having, String order, String limit, int type) {
        SQLiteDatabase db= DataBaseHelper.getInstance(context).getReadableDatabase();
        return  DbUtils.queryByTwoSelecionBase(db, this.tableName, selectionRow, selectionArgs, groupby, having, order, limit, type);
    }

    public  long insertSigle(Context context, ContentValues value) {
        List<ContentValues> values=new ArrayList<ContentValues>();
        values.add(value);
        return insertAll(context, values);
    }

    public  long insertAll(Context context, List<ContentValues> values) {
        SQLiteDatabase db= DataBaseHelper.getInstance(context).getWritableDatabase();
        return DbUtils.insert(db, this.tableName, values);
    }

    /**
     * @param countRow       需要计数的列
     * @param selection2     条件语句
     * @param selectionArgs2 参数
     * @return
     */
    public int queryCountBySelection(Context context, String countRow, String selection2, String[] selectionArgs2) {
        SQLiteDatabase db= DataBaseHelper.getInstance(context).getReadableDatabase();
        return DbUtils.queryCountBySelection(db, this.tableName, countRow, selection2, selectionArgs2);
    }

    public void closeCursor(Cursor cursor){
        DbUtils.closeCursor(cursor);
    }

    public int update(Context context, ContentValues values, String selection, String[]  selectionArgs){
        return DataBaseHelper.getInstance(context).getWritableDatabase().update(this.tableName, values, selection, selectionArgs);
    }

    public int delete(Context context, String selection, String[]  selectionArgs){
        return DataBaseHelper.getInstance(context).delete(this.tableName, selection, selectionArgs);
    }

    public  int deleteBy1Selection(Context context, String values, String selectionRow){
        return deleteByTwoSelection(context,new String[] {selectionRow},new String[]{ values});
    }

    public  int deleteByTwoSelection(Context context, String[] selectionRow, String[] selectionArgs) {
        StringBuilder selection = new StringBuilder();
        selection.append(selectionRow[0]).append("=? ");
        if (selectionRow.length > 1) {
            selection.append(" AND ").append(selectionRow[1]).append("=? ");
        }
        return DataBaseHelper.getInstance(context).delete(this.tableName, selection.toString(), selectionArgs);
    }

}
